clear all 
set more off 
set maxvar 15000 
clear matrix

**********************************************************************************************************************************************
**********************************************************************************************************************************************
**** IGE VS RANK OVER TIME
**********************************************************************************************************************************************
**********************************************************************************************************************************************
	
	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
	keep if baseline_sample==1
	
	forval i=1(1)4 {
		gen est_`i'=. 
		gen est_lb_`i' =.
		gen est_ub_`i' =.
	}
	
	levelsof decade, local(decades)		

*1. IGE
	foreach x of local decades {
		di "`x'"
				
		reg log_son_baseline log_father_baseline if decade==`x' [pw=wgt_sex_race], robust 
			replace est_1 = _b[log_father_baseline] if decade==`x'
			replace est_ub_1 = _b[log_father_baseline]+1.96*_se[log_father_baseline] if decade==`x'
			replace est_lb_1 = _b[log_father_baseline]-1.96*_se[log_father_baseline] if decade==`x'
		}

*2. Rank
	foreach x of local decades {
		di "`x'"
		
		
		reg rank_son_baseline rank_father_baseline if decade==`x' [pw=wgt_sex_race], robust  
			replace est_2 = _b[rank_father_baseline] if decade==`x'
			replace est_ub_2 = _b[rank_father_baseline]+1.96*_se[rank_father_baseline] if decade==`x'
			replace est_lb_2 = _b[rank_father_baseline]-1.96*_se[rank_father_baseline] if decade==`x'
	}

*3. IGC 
	foreach x of local decades {
		di "`x'"
		
		sum log_father_baseline if decade==`x' [aw=wgt_sex_race]
		local sig_x = `r(sd)'
		
		sum log_son_baseline if decade==`x' [aw=wgt_sex_race]
		local sig_y = `r(sd)'
		
		local ratio1 = `sig_x'/`sig_y'
		display `ratio1'
			
		reg log_son_baseline log_father_baseline if decade==`x' [pw=wgt_sex_race], robust 
			
		lincom log_father_baseline * `ratio1'
		
			replace est_3 = `r(estimate)' if decade==`x'
			replace est_ub_3 = `r(estimate)'+1.96*`r(se)' if decade==`x'
			replace est_lb_3 = `r(estimate)'-1.96*`r(se)' if decade==`x'
							
		reg log_son_baseline log_father_baseline if decade==`x' [pw=wgt_sex_race], robust beta 

	}	
	 
	
	bysort decade: keep if _n==1
	keep decade est_*
	
	reshape long est_ est_lb_ est_ub_, i(decade) j(estimate)
	replace decade= decade+2 if estimate==2
	replace decade= decade+4 if estimate==3
	
	
*-----------*
* FIGURES
*-----------*

	* IGE, rank, and IGC
	#delimit ;
	twoway (scatter est_ decade if estimate==1,  msymbol(circle) mcolor(midblue) msize(medium) yaxis(1)) 
		   (rcap est_lb_  est_ub_  decade if estimate==1, lpatter(solid) lcolor(midblue) yaxis(1) lwidth(0.5))
		   (scatter est_ decade if estimate==2,  msymbol(diamond) mcolor(orange) msize(small) yaxis(2)) 
		   (rcap est_lb_  est_ub_  decade if estimate==2, lpatter(solid) lcolor(orange) yaxis(2) lwidth(0.5) )
		   (scatter est_ decade if estimate==3,  msymbol(triangle) mcolor(navy) msize(small) yaxis(2)) 
		   (rcap est_lb_  est_ub_  decade if estimate==3, lpatter(solid) lcolor(navy) yaxis(2) lwidth(0.5) ),
	xti(" " "Decade of respondent's birth") xlabel(1910(10)1970) xscale(range(1905 1975))
	ylabel(0(.25)1, axis(1)) yti("IGE coefficient" " ", axis(1)) legend(on ring(0) row(1) pos(8) order(1 "IGE" 3 "Rank" 5 "IGC"))
	ylabel(0.15(.1).45, axis(2)) yti(" " "Rank coefficient and IGC", axis(2))
	xlabel(1910 "1910s" 1920 "1920s" 1930 "1930s" 1940 "1940s" 1950 "1950s" 1960 "1960s" 1970 "1970s", labsize(small) ) ;  
	#delimit cr
	graph export "$Mydirectory2/main_figures_tables/figure1.pdf", as(pdf) replace
